﻿namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class IMG_AddingNoise_Restoration
    {
        /// <summary>
        /// Add a periodic Noise.
        /// </summary>
        /// <param name="orig_Img">origional imnage.</param>
        /// <param name="amplitude">the amplitude.</param>
        /// <param name="fx">Frequency in x.</param>
        /// <param name="fy">Frequency in y.</param>
        /// <param name="px">Phase shift in x.</param>
        /// <param name="py">Phase shift in y.</param>
        /// <returns>Edited image normalized.</returns>
        public static Entities.IMG_Image AddPeriodicNoise(Entities.IMG_Image orig_Img, float amplitude, float fx, float fy, float px, float py)
        {
            try
            {
                Entities.IMG_Image noise_Buffer = new Entities.IMG_Image(orig_Img.Width, orig_Img.Height);
                Entities.IMG_Image noisy_Image = new Entities.IMG_Image(orig_Img.Width, orig_Img.Height);
                double value;
                double final_Value;

                for (int i = 0; i < noise_Buffer.Width; i++)
                {
                    for (int j = 0; j < noise_Buffer.Height; j++)
                    {
                        value = ((2 * Math.PI * i * fx) / orig_Img.Width) + px + ((2 * Math.PI * j * fy) / orig_Img.Height) + py;
                        final_Value = amplitude * Math.Sin(value);
                        noise_Buffer[j, i] = new Entities.Pixel(final_Value, final_Value, final_Value);
                    }
                }

                for (int i = 0; i < noisy_Image.Height; i++)
                {
                    for (int j = 0; j < noisy_Image.Width; j++)
                    {
                        noisy_Image[i, j] = new Entities.Pixel(orig_Img[i, j].Red + noise_Buffer[i, j].Red, orig_Img[i, j].Green + noise_Buffer[i, j].Green, orig_Img[i, j].Blue + noise_Buffer[i, j].Blue);
                    }
                }

                Entities.IMG_Image normalized_Noisy_Image = new Entities.IMG_Image(orig_Img.Width, orig_Img.Height);
                normalized_Noisy_Image = IMG_Normalize.IMG_NormalizeImage(noisy_Image);
                return normalized_Noisy_Image;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }
    }
}
